package com.keyidea.jfxui.core.system;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.google.gson.JsonObject;
import com.keyidea.jfxui.core.config.log.BusinessType;
import com.keyidea.jfxui.core.config.log.Log;
import com.keyidea.jfxui.core.constant.UserConstants;
import com.keyidea.jfxui.core.system.domain.AjaxResult;
import com.keyidea.jfxui.core.page.TableDataInfo;
import com.keyidea.jfxui.core.system.sqlite.entity.SysConfig;
import com.keyidea.jfxui.core.system.sqlite.mapper.SysConfigMapper;
import com.keyidea.jfxui.core.util.StringUtils;
import feign.Param;
import feign.QueryMap;
import feign.RequestLine;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 参数配置 信息操作处理
 * <p>
 * ruoyi
 */
@Service
@RequiredArgsConstructor
public class SysConfigService extends BaseService {

    private final SysConfigMapper sysConfigMapper;

    /**
     * 获取参数配置列表
     */
    public TableDataInfo list(@QueryMap Map<String, Object> map) {
        SysConfig bean = BeanUtil.toBean(map, SysConfig.class);
        startPage(map);
        List<SysConfig> sysConfigs = sysConfigMapper.selectConfigList(bean);
        return getDataTable(sysConfigs);
    }

    public void export(SysConfig config) {

    }

    /**
     * 根据参数编号获取详细信息
     */
    public AjaxResult getInfo(@Param("configId") Long configId) {
        return success(sysConfigMapper.selectConfigById(configId));
    }

    /**
     * 根据参数键名查询参数值
     */
    public AjaxResult getConfigKey(@Param("configKey") String configKey) {
        SysConfig config = new SysConfig();
        config.setConfigKey(configKey);
        SysConfig retConfig = sysConfigMapper.selectConfig(config);
        return success(retConfig.getConfigValue());
    }

    /**
     * 新增参数配置
     */
    public AjaxResult add(SysConfig config) {
        if (!checkConfigKeyUnique(config)) {
            return error("新增参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        config.setCreateBy(getUsername());
        return toAjax(sysConfigMapper.insertConfig(config));
    }

    /**
     * 修改参数配置
     */
    @Log(title = "参数配置", businessType = BusinessType.UPDATE)
    public AjaxResult edit(SysConfig config) {
        if (!checkConfigKeyUnique(config)) {
            return error("修改参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        config.setUpdateBy(getUsername());
        return toAjax(sysConfigMapper.updateConfig(config));
    }

    /**
     * 删除参数配置
     */
    public AjaxResult remove(@Param("configIds") String configIds) {
        sysConfigMapper.deleteConfigByIds(Convert.convert(Long[].class, StrUtil.splitTrim(configIds, ",")));
        return success();
    }

    /**
     * 刷新参数缓存
     */
    public JsonObject refreshCache() {
        return null;
    }

    public boolean checkConfigKeyUnique(SysConfig config) {
        Long configId = StringUtils.isNull(config.getConfigId()) ? -1L : config.getConfigId();
        SysConfig info = sysConfigMapper.checkConfigKeyUnique(config.getConfigKey());
        if (StringUtils.isNotNull(info) && info.getConfigId().longValue() != configId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}
